/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.asseco.mio.validators;

import com.asseco.mio.BasicInfo;
import com.asseco.persistence.service.HibernateUtil;
import java.util.GregorianCalendar;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author konstantin.sivakov
 */
public class EMBGValidator {

    public int checkEMBG(String embg) {

        if (embg.length() != 13) {
            return 0;
        }
        int day = Integer.parseInt(embg.substring(0, 2));
        int month = Integer.parseInt(embg.substring(2, 4));
        int year = Integer.parseInt(embg.substring(4, 7));

        int pom = 0;
        int fullYear = 0;


        if ((day == 0) || (day == 31) || (month == 0) || (month > 12)) {
            return 0;
        }

        if (((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && day > 31) {
            return 0;
        }

        if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && day > 30) {
            return 0;
        }

        if (month == 2) {
            if (Integer.parseInt(embg.substring(4, 5)) > 7) {
                pom = 1;
            } else {
                pom = 2;
            }

            fullYear = Integer.parseInt(String.valueOf(pom).concat(String.valueOf(year)));
            GregorianCalendar cal = new GregorianCalendar();

            if (cal.isLeapYear(fullYear)) {
                if (day > 29) {
                    return 0;
                }

            }
        }
        if (!isModul12(embg)) {
            return 0;
        }
        
        return 1;
    }

//    public boolean isModul11(String s){
//        
//        int y = 6;
//     
//        int sum = 0;
//        for (int i = 0; i <= 11; i++) {
//            sum=sum+Integer.parseInt(s.substring(i,i+1))*y;
//            y--;
//            if(y<2)
//                y=7;
//        }
//        sum = 11 - sum%11;
//        if (sum ==10 || sum == 11){
//                sum = 0;
//        }
//        
//        if (sum != Integer.parseInt(s.substring(12)))
//            return false;
//    
//    return true;
//    }
    public boolean isModul12(String s) {

        String firstHalf = s.substring(0, 6);
        String secondHalf = s.substring(6, 12);

        int sum = 0;

        for (int i = 0; i <= 5; i++) {
            sum += (Integer.parseInt(firstHalf.substring(i, i + 1)) + Integer.parseInt(secondHalf.substring(i, i + 1))) * (7 - i);

        }
        sum = 11 - sum % 11;
        if (sum == 10 || sum == 11) {
            sum = 0;
        }

        if (sum != Integer.parseInt(s.substring(12))) {
            return false;
        }

        return true;
    }

    public boolean exsistEMBG(String embg) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        Criteria criteria = session.createCriteria(BasicInfo.class).add(Restrictions.eq("socScrNo", embg));
        if (!criteria.list().isEmpty()) {
            return false;
        }
        return true;
    }
//    public boolean isModul9(String s) {
//        long lastTwo = Long.parseLong(s.substring(11));
//        long firstPart = Long.parseLong(s.substring(0, 11));
//        long k = 98 - (firstPart * 100) % 97;
//        if (lastTwo != k) {
//            return false;
//        } else {
//            return true;
//        }
//    }
}
